package cn.source.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.source.school.domain.School;
import cn.source.school.service.ISchoolService;
import cn.source.system.domain.GkGradeDoc;
import cn.source.system.domain.SchoolPlan;
import cn.source.system.service.IGkGradeDocService;
import cn.source.system.service.ISchoolPlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.system.domain.SchoolZyzYearAdmission;
import cn.source.system.service.ISchoolZyzYearAdmissionService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * 年度学校各专业组录取情况Controller
 *
 * @author sourcebyte.vip
 * @date 2025-06-01
 */
@RestController
@RequestMapping("/system/schoolZyzYearAdmission")
public class SchoolZyzYearAdmissionController extends BaseController
{
    @Autowired
    private ISchoolZyzYearAdmissionService schoolZyzYearAdmissionService;

    @Autowired
    private ISchoolPlanService schoolPlanService;


    @Autowired
    private IGkGradeDocService gkGradeDocService;

    @Autowired
    private ISchoolService schoolService;

    /**
     * 查询年度学校各专业组录取情况列表
     */
    @PreAuthorize("@ss.hasPermi('system:schoolZyzYearAdmission:list')")
    @GetMapping("/list")
    public TableDataInfo list(SchoolZyzYearAdmission schoolZyzYearAdmission)
    {
        startPage();
        List<SchoolZyzYearAdmission> list = schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionList(schoolZyzYearAdmission);
        return getDataTable(list);
    }

    /**
     * 获取年度学校各专业组录取情况详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionById(id));
    }

    /**
     * 新增年度学校各专业组录取情况
     */
    @PreAuthorize("@ss.hasPermi('system:schoolZyzYearAdmission:add')")
    @Log(title = "年度学校各专业组录取情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolZyzYearAdmission schoolZyzYearAdmission)
    {
        return toAjax(schoolZyzYearAdmissionService.insertSchoolZyzYearAdmission(schoolZyzYearAdmission));
    }

    /**
     * 修改年度学校各专业组录取情况
     */
    @PreAuthorize("@ss.hasPermi('system:schoolZyzYearAdmission:edit')")
    @Log(title = "年度学校各专业组录取情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolZyzYearAdmission schoolZyzYearAdmission)
    {
        int returnVal=schoolZyzYearAdmissionService.updateSchoolZyzYearAdmission(schoolZyzYearAdmission);

        List<School> list = schoolService.selectByScodeschool(schoolZyzYearAdmission.getsCode());
        Long province=null;
        if(list!=null&&list.size()>0) {
            School school=list.get(0);
            province=school.getSprovince();
        }

        List<GkGradeDoc> gradeList=null;
        if ("450000".equals(province)) {//广西院校，用广西排名

            //广西排位分
            GkGradeDoc gkGradeDoc = new GkGradeDoc();
            gkGradeDoc.setYear(schoolZyzYearAdmission.getYear());
            if ("理工类".equals(schoolZyzYearAdmission.getMajorType())) {
                gkGradeDoc.setGradeClass("工类");
            }
            if ("文史类".equals(schoolZyzYearAdmission.getMajorType())) {
                gkGradeDoc.setGradeClass("史类");
            }
            gkGradeDoc.setUseIn("广西");
            gradeList = gkGradeDocService.selectGkGradeDocList(gkGradeDoc);
        }else{
            //全国排位分
            GkGradeDoc gkGradeDoc=new GkGradeDoc();
            gkGradeDoc.setYear(schoolZyzYearAdmission.getYear());
            if("理工类".equals(schoolZyzYearAdmission.getMajorType())) {
                gkGradeDoc.setGradeClass("工类");
            }
            if("文史类".equals(schoolZyzYearAdmission.getMajorType())) {
                gkGradeDoc.setGradeClass("史类");
            }
            gkGradeDoc.setUseIn("全国");
            gradeList=gkGradeDocService.selectGkGradeDocList(gkGradeDoc);
        }

        //更新plan
        SchoolPlan schoolPlanQuery=new SchoolPlan();
        schoolPlanQuery.setMajorType(schoolZyzYearAdmission.getMajorType());
        schoolPlanQuery.setSchoolCode(schoolZyzYearAdmission.getsCode());
        schoolPlanQuery.setZyz(schoolZyzYearAdmission.getZyz());

        schoolPlanQuery.setYear(schoolZyzYearAdmission.getYear());
        schoolPlanQuery.setZyzPlan(schoolZyzYearAdmission.getPlan());
        schoolPlanQuery.setZyzAdmin(schoolZyzYearAdmission.getAdmission());
        schoolPlanQuery.setZyzAvgGrade(schoolZyzYearAdmission.getAvgGrade());

        schoolPlanQuery.setZyzLowGrade(schoolZyzYearAdmission.getLowGrade());


        Integer low = schoolZyzYearAdmission.getLowGrade();
        if(low==null) low=0;
        Integer avg = schoolZyzYearAdmission.getAvgGrade();
        if(avg==null) avg=0;


        for (int k = 0; k < gradeList.size(); k++) {
            GkGradeDoc gradeDoc = gradeList.get(k);
            if (gradeDoc.getGrade().intValue() == low.intValue()) {
                if(schoolZyzYearAdmission.getLowGradeSeq()!=null) {
                    schoolPlanQuery.setZyzLowPm(schoolZyzYearAdmission.getLowGradeSeq());
                }else{
                    schoolPlanQuery.setZyzLowPm(gradeDoc.getSeq());
                }
            }
            if (gradeDoc.getGrade().intValue() == avg.intValue()) {
                schoolPlanQuery.setZyzAvgPm(gradeDoc.getSeq());
            }
        }

        schoolPlanService.updateSchoolPlanZyzButch(schoolPlanQuery);

        return toAjax(returnVal);
    }

    /**
     * 删除年度学校各专业组录取情况
     */
    @PreAuthorize("@ss.hasPermi('system:schoolZyzYearAdmission:remove')")
    @Log(title = "年度学校各专业组录取情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schoolZyzYearAdmissionService.deleteSchoolZyzYearAdmissionByIds(ids));
    }

    /**
     * 导出年度学校各专业组录取情况列表
     */
    @PreAuthorize("@ss.hasPermi('system:schoolZyzYearAdmission:export')")
    @Log(title = "年度学校各专业组录取情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SchoolZyzYearAdmission schoolZyzYearAdmission)
    {
        List<SchoolZyzYearAdmission> list = schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionList(schoolZyzYearAdmission);
        ExcelUtil<SchoolZyzYearAdmission> util = new ExcelUtil<SchoolZyzYearAdmission>(SchoolZyzYearAdmission.class);
        util.exportExcel(response, list, "年度学校各专业组录取情况数据");
    }
}
